package pl.leader100.rest;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Request;
import javax.ws.rs.core.UriInfo;

import pl.leader100.rest.data.Doc;
import pl.leader100.rest.data.DocDao;
import pl.leader100.rest.data.Situation;

// The class registers its methods for the HTTP GET request using the @GET annotation. 
// Using the @Produces annotation, it defines that it can deliver several MIME types,
// text, XML and HTML. 

// The browser requests per default the HTML MIME type.

//Sets the path to base URL + /hello
@Path("/situation")
public class ListSituations {
	private static final String year = "2011/2012";
	private static final String course = "junior_leader_r1";

	@Context
	UriInfo uriInfo;
	@Context
	Request request;

	// This method is called if TEXT_PLAIN is request
	/*
	 * @GET
	 * 
	 * @Produces(MediaType.TEXT_PLAIN) public String sayPlainTextHello() {
	 * return "no way"; }
	 */

	// For the browser
	@GET
	@Produces(MediaType.TEXT_XML)
	public List<Doc> getDocsBrowser() {
		List<Doc> docs = new ArrayList<Doc>();
		docs.addAll(DocDao.GetInstance().getModel().values());
		return docs;
	}

	// Application integration
	@GET
	@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
	public List<Doc> getDoc() {
		List<Doc> docs = new ArrayList<Doc>();
		docs.addAll(DocDao.GetInstance().getModel().values());
		return docs;
	}

	@Path("/{month}/{locale}/{course}")
	public ListSituation getSituation(@PathParam("month") String month,
			@PathParam("locale") String locale,
			@PathParam("course") String course) {

		if ("".equals(month))
			month = "*";
		else
			;// date = date.split("/")[1];

		if ("".equals(locale))
			locale = "*";
		else
			;// locale = locale.split("/")[1];
		return new ListSituation(uriInfo, request, month, locale, "2011/2012",
				course);
	}

	@GET
	@Produces(MediaType.TEXT_HTML)
	@Path("/links3/{locale}/{course}/{field}")
	public String getLinks3(@PathParam("locale") String locale,
			@PathParam("course") String course, @PathParam("field") String field) {

		if ("".equals(locale))
			locale = "*";

		try {
			DateFormat df = new SimpleDateFormat("MM");
			
			Date d = new Date();
			Calendar cal = Calendar.getInstance();
			cal.setTime(d);
			cal.set(Calendar.DAY_OF_MONTH, 15);
			String actualMonth = df.format(cal.getTime());

			String[] situations = DocDao.GetInstance().getSituations(field,
					Integer.parseInt(actualMonth), locale, year, course);
			if (situations == null)
				throw new RuntimeException("Get: Situation with " + actualMonth+ " "
						+ locale + " not found");
			Situation s = new Situation(course, actualMonth, locale, situations, null);
						
			String links = "";//<td class='l100_links'>";

			links += "<td class='l100_prevLinks'>";
			links += s.getCompetency_prev();
			links += "</td>";
			links += "<td class='l100_actualLinks'>";
			links += s.getCompetency_actual();
			links += "</td>";
			links += "<td class='l100_nextLinks'>";
			links += s.getCompetency_next();
			links += "</td>";

			//links += "</div>";
			return links;
		} catch (Exception e) {
		}
		return null;
	}
	@GET
	@Produces(MediaType.TEXT_PLAIN)
	@Path("/links/{locale}/{course}/{field}")
	public String getCurrCompetency(@PathParam("locale") String locale,
			@PathParam("course") String course, @PathParam("field") String field) {

		if ("".equals(locale))
			locale = "*";

		try {
			DateFormat df = new SimpleDateFormat("MM");
			
			Date d = new Date();
			Calendar cal = Calendar.getInstance();
			cal.setTime(d);
			cal.set(Calendar.DAY_OF_MONTH, 15);
			String actualMonth = df.format(cal.getTime());

			String[] situations = DocDao.GetInstance().getSituations(field,
					Integer.parseInt(actualMonth), locale, year, course);
			if (situations == null)
				throw new RuntimeException("Get: Situation with " + actualMonth+ " "
						+ locale + " not found");
			Situation s = new Situation(course, actualMonth, locale, situations, null);
						
			String links = "";//<td class='l100_links'>";

			links += s.getCompetency_actual();

			//links += "</div>";
			return links;
		} catch (Exception e) {
		}
		return null;
	}
}
